package cn.itcast.service.impl;

import cn.itcast.dao.CourierDao;
import cn.itcast.dao.StandardDao;
import cn.itcast.domain.Courier;
import cn.itcast.domain.Standard;
import cn.itcast.service.CourierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//快递员业务操作层
@Service
public class CourierServiceImpl implements CourierService {

    @Autowired
    private CourierDao courierDao;

    @Autowired
    private StandardDao standardDao;


    //保存或修改快递员信息
    @Override
    @Transactional
    public void save(Courier courier) {
        courierDao.save(courier);
    }

    //查询所有标准公斤数据
    @Override
    public List<Standard> findAll() {
        List<Standard> list = standardDao.findAll();
        return list;
    }

    //分页查询
    @Override
    public Page<Courier> fandAllpage(int page, int rows, Courier courier) {
        //排序 根据id升序排序
        Sort sort =new Sort(Sort.Direction.ASC,"id");

        //构建模糊查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("courierNum", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("standard.name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("company", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.contains());
        //分页
        PageRequest pageRequest = PageRequest.of(page - 1, rows,sort);
        Example example = Example.of(courier, matcher);
        return courierDao.findAll(example,pageRequest);

    }

    //作废快递员信息
    @Override
    public void del(String ids) {
        //将传过来的字符串根据","号分割,然后转成int类型的
        String[] str = ids.split(",");
        for (String s : str) {
            int id = Integer.parseInt(s);
            courierDao.del(id);
        }

    }

    //还原快递员信息
    @Override
    public void restore(String ids) {
        //将传过来的字符串根据","号分割,然后转成int类型的
        String[] str = ids.split(",");
        for (String s : str) {
            int id = Integer.parseInt(s);
            courierDao.restore(id);
        }
    }


}
